﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

using DevExpress.Xpf.Editors.Settings;
using DevExpress.Xpf.Grid;

using Microsoft.SharePoint.Client;

using SBPweb.RIA.Client.Common.Collections;

namespace SBPweb.Sharepoint.Client.Controls.Fields
{

    public partial class NumberEditSettings : SpinEditSettings, IFieldEditSettings<FieldNumber>
    {

        public NumberEditSettings()
            : base()
        {
        }

        public static readonly DependencyProperty FieldProperty = DependencyProperty.Register("Field", typeof(FieldNumber), typeof(IFieldEditSettings<FieldNumber>), null);
        public static readonly DependencyProperty IsPercentageProperty = DependencyProperty.Register("IsPercentage", typeof(bool), typeof(IFieldEditSettings<FieldNumber>), null);
        public static readonly DependencyProperty IsCurrencyProperty = DependencyProperty.Register("IsCurrency", typeof(bool), typeof(IFieldEditSettings<FieldNumber>), null);
        public static readonly DependencyProperty CurrencyLocaleProperty = DependencyProperty.Register("CurrencyLocale", typeof(string), typeof(IFieldEditSettings<FieldNumber>), null);
        public static readonly DependencyProperty DecimalsProperty = DependencyProperty.Register("Decimals", typeof(int?), typeof(IFieldEditSettings<FieldNumber>), null);

        public FieldNumber Field
        {
            get
            {
                return GetValue(FieldProperty) as FieldNumber;
            }
            set
            {
                SetValue(FieldProperty, value);
                InitializeFromField();
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Field"));
                }
            }
        }

        public bool IsPercentage
        {
            get
            {
                return (bool)GetValue(IsPercentageProperty);
            }
            set
            {
                SetValue(IsPercentageProperty, value);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsPercentage"));
                }
            }
        }

        public bool IsCurrency
        {
            get
            {
                return (bool)GetValue(IsCurrencyProperty);
            }
            set
            {
                SetValue(IsCurrencyProperty, value);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsCurrency"));
                }
            }
        }

        public string CurrencyLocale
        {
            get
            {
                return (string)GetValue(CurrencyLocaleProperty);
            }
            set
            {
                SetValue(CurrencyLocaleProperty, value);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CurrencyLocale"));
                }
            }
        }

        public int? Decimals
        {
            get
            {
                return (int?)GetValue(DecimalsProperty);
            }
            set
            {
                SetValue(DecimalsProperty, value);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Decimals"));
                }
            }
        }

        protected virtual void InitializeFromField()
        {
            IsCurrency = Field is FieldCurrency;
            XDocument schemaXml = XDocument.Parse(Field.SchemaXml);
            XElement fieldElement = schemaXml.Element("Field");
            IsPercentage = fieldElement.AttributeValue<bool>("Percentage");
            Decimals = fieldElement.AttributeValue<int?>("Decimals");
            CurrencyLocale = fieldElement.AttributeValue<string>("LCID");
            MinValue = !Field.MinimumValue.Equals(double.MinValue) ? Convert.ToDecimal(Field.MinimumValue) : default(decimal?);
            MaxValue = !Field.MaximumValue.Equals(double.MaxValue) ? Convert.ToDecimal(Field.MaximumValue) : default(decimal?);
            
            HorizontalContentAlignment = EditSettingsHorizontalAlignment.Right;

            CultureInfo ci;
            NumberFormatInfo nfi;
            if (IsCurrency)
            {
                ci = CurrencyLocale.GetCultureByLCID();
            }
            else
            {
                ci = Thread.CurrentThread.CurrentCulture;
            }
            nfi = ci.NumberFormat;

            string dec = Decimals.HasValue ? "0".Multiply(Decimals.Value) : string.Empty;
            if (IsCurrency)
            {
                Mask = "C" + (Decimals.HasValue ? Convert.ToString(Decimals.Value) : string.Empty);
            }
            else if (IsPercentage)
            {
                Mask = string.Format("##0.{0} {1}", dec, nfi.PercentSymbol);
            }
            else
            {
                Mask = string.Format("# ### ##0.{0}", dec);
            }
            MaskType = DevExpress.Xpf.Editors.MaskType.Numeric;
            MaskCulture = ci;
            MaskUseAsDisplayFormat = true;
            Increment = IsPercentage ? (MinValue.HasValue ? (decimal)(MaxValue.Value / 100) : (decimal)0.01) : (decimal)1.0;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public override DevExpress.Xpf.Editors.IBaseEdit CreateEditor(bool assignEditorSettings, IDefaultEditorViewInfo defaultViewInfo)
        {
            DevExpress.Xpf.Editors.SpinEdit ret = base.CreateEditor(assignEditorSettings, defaultViewInfo) as DevExpress.Xpf.Editors.SpinEdit;

            //Binding binding = new Binding("UsersSource");
            //binding.Source = this;
            //ret.SetBinding(DevExpress.Xpf.Editors.ComboBoxEdit.ItemsSourceProperty, binding);

            //ret.Loaded += new RoutedEventHandler(ret_Loaded);

            return ret;
        }

        //private void ret_Loaded(object sender, RoutedEventArgs e)
        //{
        //}

        //    //editor = new UserTagEditor();
        //    //if (assignEditorSettings)
        //    //{
        //    //    AssignToEdit(editor, defaultViewInfo);
        //    //}
        //    editor = base.CreateEditor(assignEditorSettings, defaultViewInfo) as ComboBoxEdit;
        //    //Binding binding = new Binding("Column");
        //    //binding.Converter = new UserTagItemsConverter();
        //    //binding.ConverterParameter = this.Name;
        //    //editor.SetBinding(ComboBoxEdit.ItemsSourceProperty, binding);
        //    editor.Loaded += new RoutedEventHandler(editor_Loaded);
        //    return editor;
        //}

        //void editor_Loaded(object sender, RoutedEventArgs e)
        //{
        //    //if (editor.EditValue != null)
        //    //{
        //    //    editor.ItemsSource = new FieldUserValue[] { editor.EditValue as FieldUserValue };
        //    //}
        //    //editor.Items.Add(editor.DataContext
        //}

    }

}